$(function () {
  var form = layui.form
  var laypage = layui.laypage,
    layer = layui.layer
  //发起获取文章分类请求
  function getfenlei() {
    $.ajax({
      url: '/my/article/cates',
      type: 'get',
      data: {},
      success: function (res) {
        // console.log(res, 88888)
        var html = template('opt', res)
        $('#cates').html(html)
        form.render()
      }
    })
  }
  getfenlei()

  //渲染列表
  function getlist(option) {
    $.ajax({
      url: '/my/article/list',
      type: 'get',
      data: option,
      success: function (res) {
        console.log(res, 1111222222)
        total = res.total
        var html = template('tpl', res)
        $('tbody').html(html)
      }
    })
  }
  // getlist()
  getlist({ pagenum: 1, pagesize: 5 })

  // 筛选
  $('.choose').on('click', function (e) {
    e.preventDefault()
    var cate_id = $('#cates').val()
    var state = $('#state').val()
    // console.log(cate_id,state);
    getlist({ pagenum: 1, pagesize: 5, cate_id: cate_id, state: state })
  })

  //分页

  function fenye() {
    $.ajax({
      url: '/my/article/list',
      type: 'get',
      data: {
        pagenum: 1,
        pagesize: 5
      },
      success: function (res) {
        console.log(res, 111111)
        let total = res.total
        console.log(total)
        laypage.render({
          elem: 'demo7',
          count: total,
          layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
          jump: function (obj) {
            const { curr: pagenum, limit: pagesize } = obj
            // console.log(pagenum, pagesize,6666)
            getlist({ pagenum, pagesize })
          }
        })
      }
    })
  }
  fenye()
})
